package com.jiang.thread.thread;

import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class ThreadCreate {

	// 如果 for 循环（主线程）在前面，先执行完主线程，再执行子线程 thread01
	@Test
	public void test1(){
		for (int i = 0; i < 1000; i++) {
			System.out.println("主线程 " + i);
		}

		Thread01 thread1 = new Thread01();
		thread1.start();
	}

	@Test
	public void test2(){
		// 子线程放在前面，子线程开始执行，并会伴同 for 循环（主线程）一起交替执行，也就是多线程效果
		Thread01 thread1 = new Thread01();
		thread1.start();

		Thread02 thread2 = new Thread02();
		new Thread(thread2).start();

		Thread03 thread3 = new Thread03();
		FutureTask<Integer> ft = new FutureTask<>(thread3);
		new Thread(ft).start();

		for (int i = 0; i < 1000; i++) {
			System.out.println("主线程 " + i);
		}
	}
}

// 继承 Thread 类
class Thread01 extends Thread{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 1000; i++) {
			System.out.println(this.getName() + " " + i);
		}
	}
}

// 实现 Runnable 接口
class Thread02 implements Runnable{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 1000; i++) {
			System.out.println(Thread.currentThread().getName() + " " + i);
		}
	}
}

// 实现 Callable 接口
class Thread03 implements Callable<Integer>{
	@Override
	public Integer call() throws Exception {
		for (int i = 0; i < 1000; i++) {
			System.out.println(Thread.currentThread().getName() + " " + i);
		}
		return 0;
	}
}





